import request from '@/utils/request'

export interface MaterialQuery {
  keyword?: string
  category?: string
  status?: string
  page: number
  pageSize: number
}

export interface MaterialItem {
  id: string
  name: string
  code: string
  spec: string
  category: string
  stock: number
  unit: string
  status: string
  updateTime: string
}

export interface StatisticsData {
  total: {
    count: number
    change: number
  }
  lowStock: {
    count: number
    change: number
  }
  todayIn: {
    count: number
    change: number
  }
  todayOut: {
    count: number
    change: number
  }
}

// 获取物料统计数据
export function getStatistics() {
  return request({
    url: '/api/material/statistics',
    method: 'get'
  })
}

// 获取物料列表
export function getMaterialList(query: MaterialQuery) {
  return request({
    url: '/api/material/list',
    method: 'get',
    params: query
  })
}

// 创建物料
export function createMaterial(data: Partial<MaterialItem>) {
  return request({
    url: '/api/material',
    method: 'post',
    data
  })
}

// 更新物料
export function updateMaterial(id: string, data: Partial<MaterialItem>) {
  return request({
    url: `/api/material/${id}`,
    method: 'put',
    data
  })
}

// 删除物料
export function deleteMaterial(id: string) {
  return request({
    url: `/api/material/${id}`,
    method: 'delete'
  })
}

// 获取物料类别列表
export function getCategoryList() {
  return request({
    url: '/api/material/categories',
    method: 'get'
  })
}

// 入库记录
export function createStockIn(data: { materialId: string; quantity: number; remark?: string }) {
  return request({
    url: '/api/material/stock-in',
    method: 'post',
    data
  })
}

// 出库记录
export function createStockOut(data: { materialId: string; quantity: number; remark?: string }) {
  return request({
    url: '/api/material/stock-out',
    method: 'post',
    data
  })
} 